// pages/recharge/recharge.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    val: null,//充值金额
    oftenNum: [50, 100, 200, 300, 500, 1000],
    active: -1,
    text: null //充值框的内容
  },

  /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {
    wx.request({
      url: app.baseURL + 'Mine&a=award&user_id=' + app.globalData.userId,
      success: (res) => {
        this.setData({
          oftenNum: res.data.list
        })
      }
    })
  },

  //选择充值金额
  selectMoney(e) {
    const index = e.currentTarget.dataset.index;
    const value = e.currentTarget.dataset.value.money;
    this.setData({
      active: index,
      val: value,
      text: value
    })
  },
  //输入框获取焦点时，清除选中的充值金额
  textFocus(e) {
    this.setData({
      active: -1,
      val: e.detail.value
    })
  },
  //监听输入值改变
  valueChange(e) {
    console.log(e);
    this.setData({
      val: e.detail.value
    });
  },

  //点击充值
  clickRecharge() {
    let val = this.data.val;
    console.log()
    if (!val) { wx.showToast({ title: '请输入充值金额', icon: 'none' }); return };
    console.log(val)
    //进行提交数据
    wx.request({
      url: app.baseURL + 'Mine&a=recharge',
      data: {
        user_id: app.globalData.userId,
        money: val,
        type: 2
      },
      success: (res) => {
        if (res.data.status == 1) {
          const wxPay = res.data.res;
          wx.requestPayment({
            'timeStamp': wxPay.timeStamp,
            'nonceStr': wxPay.nonceStr,
            'package': wxPay.packages,
            'signType': 'MD5',
            'paySign': wxPay.sign,
            'success': (res) => {
              wx.navigateTo({ url: '../rechargeResult/rechargeResult' })
            },
            'fail': (res) => {
              console.log(res)
              wx.showToast({ title: '网络错误，请稍后重试', icon: 'none' })
            }
          })
        }
      }
    })
  }

})